package com.gcit.seniorWish.web.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gcit.seniorWish.web.common.persistence.dao.DeptMapper;
import com.gcit.seniorWish.web.common.persistence.model.Dept;
import com.gcit.seniorWish.web.modular.system.service.IDeptService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Resource
    private DeptMapper deptMapper;

    /**
    * 删除部门
    * @param deptId 要删除的部门id
    * @author gaozhichang
    * @date 2018-03-13 下午 4:45
    */
    @Override
    public void deleteDept(String deptId) {
        Dept dept = deptMapper.selectById(deptId);
        deptMapper.deleteById(deptId);
        //删除子部门
        Wrapper<Dept> wrapper = new EntityWrapper<>();
        wrapper = wrapper.like("pids", "%[" + dept.getId() + "]%");
        deptMapper.delete(wrapper);
    }


    /**
    *获取子部门的查询sql（作为子查询，适用于ew.exists方法）
    * @param deptId 父级部门
    * @author gaozhichang
    * @date 2018-03-02 下午 4:54
    */
    public static String getSubdeptIdSql(String deptId,boolean includeSelf){
        if(!includeSelf){
            return "select id from system_dept where (pids like '%["+deptId+"]%' ) and dept_id =system_dept.id";
        }else{
            return "select id from system_dept where (pids like '%["+deptId+"]%' or id="+deptId+") and dept_id =system_dept.id";
        }
    }
}
